package demo.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.SourceLocation;
import sun.rmi.transport.ObjectTable;

import java.util.Arrays;

public class JoinPointUtil {

    private JoinPointUtil() {
    }

    /**
     * 打印对象信息
     *
     * @param objects
     */
    public static void log(Object... objects) {
        if (objects == null) {
            System.out.println("null");
            return;
        }
        for (Object object : objects) {
            printObject(object);
        }
    }

    public static void printObject(Object object) {
        if (object == null) {
            System.out.println("null");
        }
        String classType = object.getClass().toString();
        System.out.println(classType + " = " + object);
    }

    public static void printJoinPoint(JoinPoint joinPoint) {
        printObject(joinPoint);
        if (joinPoint == null) {
            return;
        }
        JoinPoint.StaticPart staticPart = joinPoint.getStaticPart();
        Signature signature = staticPart.getSignature();
        System.out.println("Signature: " + signature.toLongString());
        SourceLocation sourceLocation = staticPart.getSourceLocation();
        String withinType = sourceLocation.getWithinType().getCanonicalName();
        int line = sourceLocation.getLine();
        System.out.println("SourceLocation: " + withinType + ":" + line);
        String kind = staticPart.getKind();
        System.out.println("kind: " + kind);
        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.toString(args));
        Object target = joinPoint.getTarget();
        String classType = target.getClass().toString();
        System.out.println("target: " + classType);
    }
}
